//
//  JZHAppDelegate.m
//  极致荟
//
//  Created by mac on 14-4-3.
//  Copyright (c) 2014年 mac. All rights reserved.
//

#import "JZHAppDelegate.h"
#import "JZHWelcomeViewController.h"
#import <YiXinConnection/YiXinConnection.h>
#import <ShareSDK/ShareSDK.h>
#import "YXApi.h"
#import "WXApi.h"
#import <TencentOpenAPI/QQApiInterface.h>
#import <TencentOpenAPI/TencentOAuth.h>
#import <QQConnection/QQConnection.h>
#import <WeChatConnection/WeChatConnection.h>
#import "JZHSocket.h"
#import "MessageContent.h"
@implementation JZHAppDelegate
id citydata;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
//    //设置导航背景
//    [[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"navigationbar_bg.png"] forBarMetrics:UIBarMetricsDefault];
    //
    self.view=[[UIView alloc]initWithFrame:CGRectMake(0, 0, 190, self.window.frame.size.height)];
       [self.window addSubview:self.view];
    
    if(OSVersionIsAtLeastiOS7<7.0){
        [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleBlackOpaque];
        
        UINavigationBar *bar=[UINavigationBar appearance];
        [bar setTintColor:[UIColor whiteColor]];
        [bar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor blackColor],UITextAttributeTextColor,[UIFont systemFontOfSize:16.0],UITextAttributeFont ,nil]];
        
    }
  
    //[cltimer setFireDate:[NSDate distantPast]];
    //这个是开始蓝牙工作啦
    
    //获取城市列表
    [[User defuceUser] userCity];
    
    
    //设置ShareSDK APIKEY
    [ShareSDK registerApp:@"1e6ea40c0f81"];
    
    //添加QQ微博  注册网址  http://connect.qq.com/intro/login/
    [ShareSDK connectTencentWeiboWithAppKey:@"801511270" appSecret:@"0f607227d40bf3db76417a93c07cafa1" redirectUri:@"http://www.sharesdk.cn"];
       //添加新浪微博
    [ShareSDK connectSinaWeiboWithAppKey:@"3239694235" appSecret:@"b2ad3d4e4785c4fdc9985e45c078f7ac" redirectUri:@"http://www.sharesdk.cn"];
    //添加易信
    [ShareSDK connectYiXinWithAppId:@"yx7c3935a7724a4953ab978a4e53de7744" yixinCls:[YXApi class]];
    [ShareSDK connectSMS];
    //添加微信
    
    [ShareSDK connectWeChatSessionWithAppId:@"wx6e7e9de84ed0aec0" wechatCls:[WXApi class]];
    [WXApi registerApp:@"wx6e7e9de84ed0aec0"];
    //[ShareSDK connectWeChatWithAppId:@"wx6e7e9de84ed0aec0" wechatCls:[WXApi class]];

    JZHWelcomeViewController *welcomeVC = [[JZHWelcomeViewController alloc] init];
    
    [JZHSocket deful:self];
    
    
    self.window.rootViewController = welcomeVC;
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    
    return YES;
}
- (BOOL)application:(UIApplication *)application  handleOpenURL:(NSURL *)url
{
//    return [ShareSDK handleOpenURL:url
//                        wxDelegate:self];
    return [WXApi handleOpenURL:url delegate:self];
}

- (BOOL)application:(UIApplication *)application
            openURL:(NSURL *)url
  sourceApplication:(NSString *)sourceApplication
         annotation:(id)annotation
{
//    return [ShareSDK handleOpenURL:url
//                 sourceApplication:sourceApplication
//                        annotation:annotation
//                        wxDelegate:self];
     return [WXApi handleOpenURL:url delegate:self];
}

-(void) onReq:(BaseReq*)req
{
    NSLog(@"%@",req.openID);
    
}
-(void) onResp:(BaseResp*)resp
{
     NSLog(@"%@",resp.errStr);
}
#pragma mark ------websocket代理-----
- (void)webSocketDidOpen:(SRWebSocket *)webSocket
{
    if([User defuceUser].userId>0)
    {
        NSDictionary *datadic=[[NSDictionary alloc] initWithObjectsAndKeys:[NSNumber numberWithInt:[User defuceUser].userId],@"userId" ,nil];
        NSDictionary *messgedic=[[NSDictionary alloc] initWithObjectsAndKeys:@"userOnline",@"type",datadic,@"data", nil];
        NSData *jsondata=[NSJSONSerialization dataWithJSONObject:messgedic options:NSJSONWritingPrettyPrinted error:nil];
        NSString *json=[[NSString alloc] initWithData:jsondata encoding:NSUTF8StringEncoding];
        
        [[JZHSocket deful:self] send:json];
    }
    NSLog(@"链接成功");
    cltimer=nil;
}
- (void)chonglian:(NSTimer *)time
{
      [[JZHSocket deful:self] reconnection:self];
    NSLog(@"**");
}
- (void)webSocket:(SRWebSocket *)webSocket didFailWithError:(NSError *)error
{
    NSLog(@"链接失败错误： %@", error);
    cltimer=[NSTimer timerWithTimeInterval:3.0 target:self selector:@selector(chonglian:) userInfo:nil repeats:NO];
    [[NSRunLoop  currentRunLoop] addTimer:cltimer forMode:NSDefaultRunLoopMode];
    webSocket = nil;
   
  
}

- (void)webSocket:(SRWebSocket *)webSocket didReceiveMessage:(id)message
{
    
    [MessageContent coedMessage:message];
    
}

- (void)webSocket:(SRWebSocket *)webSocket didCloseWithCode:(NSInteger)code reason:(NSString *)reason wasClean:(BOOL)wasClean
{
    NSLog(@"关闭链接啦");
    webSocket = nil;
}

- (void)applicationWillResignActive:(UIApplication *)application
{
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.

}

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
    
}

- (void)applicationWillEnterForeground:(UIApplication *)application
{
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
    
}

- (void)applicationDidBecomeActive:(UIApplication *)application
{
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    
}

- (void)applicationWillTerminate:(UIApplication *)application
{
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    
}


@end
